<?php

namespace Biz\model\Album;

use Doraemon\pockets\datebase\BaseDbModel;
use Medoo\Medoo;

class AlbumRelateTagsModel extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    public const _TABLE_NAME = 'album_relate_tags';

    const STATUS_NORMAL = 1;
    // 状态为删除
    const STATUS_DELETE = 9;

    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }

    public static function updateDataByWhere($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return true;
    }

    public static function selectByWhere($where, $field = '*')
    {
        return self::select($field, $where) ?? [];
    }

    public static function getByWhere($field, $where = [])
    {
        return  self::get($field, $where);
    }

    public static function getRelaAlbumByWhere($where){
        $condition = [];

        if($where['package_name']) {
            $condition[self::_TABLE_NAME.".package_name"] = $where['package_name'];
        }
        if($where['tag_name']) {
            $condition[self::_TABLE_NAME.".tag_name"] = $where['tag_name'];
        }
        if($where['not_album_ids']){
            $condition[self::_TABLE_NAME.".album_id[!]"] = $where['not_album_ids'];
        }
        $condition[AlbumListModel::_TABLE_NAME.".status"] = [1,2] ;
        $condition[AlbumListModel::_TABLE_NAME.".score[>=]"] = 70 ;
        $condition[self::_TABLE_NAME.".status"] = 1 ;
        $condition["ORDER"] = [AlbumListModel::_TABLE_NAME.".publish_time" => "DESC"];
        if($where['limit']){
            $condition["LIMIT"] = $where['limit'];
        }

        return self::select(
             [
                 "[>]" . AlbumListModel::_TABLE_NAME => ["album_id" => "album_id"],
             ],
             [
                 self::_TABLE_NAME . ".album_id",
                 AlbumListModel::_TABLE_NAME.".title",
                 AlbumListModel::_TABLE_NAME.".desc",
                 AlbumListModel::_TABLE_NAME.".cover_img"

             ], $condition
         );
    }


    public static function getTopicAlbum()
    {
         return self::select(
            [
                "[>]" . AlbumListModel::_TABLE_NAME => ["album_id" => "album_id"],
            ],
            [
                self::_TABLE_NAME . ".album_id",
                self::_TABLE_NAME . ".package_name",
                self::_TABLE_NAME . ".tag_name",
                AlbumListModel::_TABLE_NAME.".title",
                AlbumListModel::_TABLE_NAME.".id",
                AlbumListModel::_TABLE_NAME.".desc",
                AlbumListModel::_TABLE_NAME.".cover_img",
                'maxtime' => Medoo::raw('MAX('.AlbumListModel::_TABLE_NAME.'.publish_time)'),
            ], [
                 AlbumListModel::_TABLE_NAME.".status" => [1,2],
                 self::_TABLE_NAME.".status" => 1,
                 self::_TABLE_NAME . ".package_name"=> "主题",
                'ORDER' => ['maxtime' => 'DESC'],
                'LIMIT' =>  10,
                'GROUP' => [self::_TABLE_NAME . ".tag_name" ]
            ]
        );
    }
    
    public static function getTopicTags(){
        return self::select(
            [
                self::_TABLE_NAME . ".package_name",
                self::_TABLE_NAME . ".tag_name",
            ], [
                self::_TABLE_NAME.".status" => 1,
                self::_TABLE_NAME . ".package_name"=> "主题",
                'GROUP' => [self::_TABLE_NAME . ".tag_name" ]
            ]
        );
    }

}